package com.etongwl.xiaomishouhuanapp;

import java.io.IOException;
import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.os.Handler;
import android.os.ParcelUuid;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class LinkActivity extends Activity implements View.OnClickListener {
	// Return Intent extra
	public static String EXTRA_DEVICE_ADDRESS = "device_address";

	private BluetoothAdapter mBtAdapter;

	private String conAddress = null;

	private TextView addressTextView, linkTextView;
	private Button linkBtn;

	private Handler hander = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_link);
		addressTextView = (TextView) findViewById(R.id.textView_address);
		linkTextView = (TextView) findViewById(R.id.textView_link);
		linkBtn = (Button) findViewById(R.id.button_link);
		init();
	}

	private void init() {
		conAddress = getIntent().getStringExtra(EXTRA_DEVICE_ADDRESS);
		addressTextView.setText("选择的address is " + conAddress);
		// Get the local Bluetooth adapter
		mBtAdapter = BluetoothAdapter.getDefaultAdapter();
		linkBtn.setOnClickListener(this);
		BluetoothDevice device = mBtAdapter.getRemoteDevice(conAddress);
		  Log.e("------------", device.toString()+"--name="+device.getName()+"---bandstate="+device.getBondState()+"--"+device.getType());
		if (device.fetchUuidsWithSdp()) {
			ParcelUuid[] uuids = device.getUuids();
			if (uuids != null) {
				Log.e("------------", "连接中。。。   uuids=" + uuids.toString());
			}else{
				Log.e("------------", "连接中。。。   uuids=is null");
			}
		} else {
			Log.e("------------", "连接中。。。  not uuids");
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button_link:
			linkTextView.setText("点击开始连接。。。");
//			BluetoothDevice device = mBtAdapter.getRemoteDevice(conAddress);
//			Thread thread=new Thread(new ConnectionThread(device));
//			thread.start();
			Thread thread = new Thread() {
				@Override
				public void run() {
					conection();
				}
			};
			thread.start();
			break;

		default:
			break;
		}

	}

	private void conection() {
//		final String SPP_UUID = "B61550DB-8496-ADB7-FF4E-4EE5FFCD35EE";
		final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";
		UUID uuid = UUID.fromString(SPP_UUID);
		BluetoothDevice device = mBtAdapter.getRemoteDevice(conAddress);
		
		BluetoothSocket socket = null;
		try {
			socket = device.createInsecureRfcommSocketToServiceRecord(uuid);
			mBtAdapter.cancelDiscovery();
			socket.connect();
			hander.post(new Runnable() {

				@Override
				public void run() {
					linkTextView.setText("连接成功！");
				}
			});
		} catch (IOException e) {
			e.printStackTrace();
			// Close the socket
			try {
				if (socket != null) {
					socket.close();
				}
			} catch (IOException e2) {
				Log.e(LinkActivity.class.getName(),
						"unable to close() socket during connection failure",
						e2);
			}
			hander.post(new Runnable() {

				@Override
				public void run() {
					linkTextView.setText("连接失败");
				}
			});
			return;

		}

	}

}
